<?php
include_once "xAP-functions/webincludes.php";
set_time_limit(0); // run forever
echo webpageheader();

if (file_exists($config["basefolder"]."/import/switchboard.mdb")==False){

echo "Place the switchboard database in ".$config["basefolder"]."/import and call it switchboard.mdb";
}
else
{
$db_connection = odbc_connect("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".$config["basefolder"]."/import/switchboard.mdb", "ADODB.Connection", "password", "SQL_CUR_USE_ODBC");

$result = odbc_tables($db_connection);

while (odbc_fetch_row($result)) {
    if(odbc_result($result,"TABLE_TYPE")=="TABLE") {
        echo "<br />Table found " . odbc_result($result,"TABLE_NAME");
    }
}
flush();
dbtransactionstart();
//transfer contacts
$i=0;
echo "<HR>Importing Contacts<BR>";
$result=odbc_exec($db_connection,"select * from xAP_Contacts");
dbexec("delete from xAP_Contacts");
while ($row = odbc_fetch_array($result)){
	echo ". ";
	$sql="insert into xAP_Contacts (";
	$neededrows=array_keys($row);
	$neededrowsclean=array_keys($row);
	array_walk($neededrows,'stripit');//clean spaces
	unset($neededrows[0]); // remove PK
	unset($neededrowsclean[0]); // remove PK
	unset($neededrows[47]); //remove TTY bad field
	unset($neededrowsclean[47]);//remove TTY bad field
	
	$sql.="".implode(", ",$neededrows).")  VALUES (";
	foreach($neededrowsclean as $key=>$value){
		$sql.="'".dbesc($row[$value])."',";
	}
	$sql=substr($sql,0,strlen($sql)-1); // strip last comma
	$sql.=")";
	flush();
	dbexec($sql);
	$i++;
	//die();
};
echo "<BR>Imported ".$i." contacts";
dbtransactionend();
dbtransactionstart();
echo "<HR>Importing Log<BR>";
$i=0;
$result=odbc_exec($db_connection,"select * from xAP_Telephone_Log");
dbexec("delete from xAP_Telephone_Log");
while ($row = odbc_fetch_array($result)){
	echo ". ";
	$sql="insert into xAP_Telephone_Log 
	(CallName,CallNumber,StrippedNumber,RNNumber,CallType,Company,Lookup,Pic,Location,STD,CallDate,Direction,Source,LookupSource,Answered,Duration,LookupRef,Rings,MapRef,NationalSTD,RawNumber,Network) VALUES (
	'".dbesc($row["Name"])."',
	'".dbesc($row["Number"])."',
	'".dbesc($row["StrippedNumber"])."',
	'".dbesc($row["RNNumber"])."',
	'".dbesc($row["Type"])."',
	'".dbesc($row["Company"])."',
	'".dbesc($row["Lookup"])."',
	'".dbesc($row["Pic"])."',
	'".dbesc($row["Location"])."',
	'".dbesc($row["STD"])."',
	'".dbesc($row["DateTime"])."',
	'".dbesc($row["Direction"])."',
	'".dbesc($row["Source"])."',
	'".dbesc($row["LookupSource"])."',
	'".dbesc($row["Answered"])."',
	'".dbesc($row["Duration"])."',
	'".dbesc($row["LookupRef"])."',
	'".dbesc($row["Rings"])."',
	'".dbesc($row["MapRef"])."',
	'".dbesc($row["NationalSTD"])."',
	'".dbesc($row["RawNumber"])."',
	'".dbesc($row["Network"])."'
	)";
	$i++;
	flush();
	dbexec($sql);
	}
}
echo "<BR>Imported ".$i." log";
dbtransactionend();

echo webpagefooter();
	dbexec("VACUUM;");
function stripit(&$item1, $key)
{
    $item1=str_replace(" ","",$item1);
	$item1=str_replace("-","",$item1);
	$item1=str_replace("'","",$item1);
}
?>